const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifTeacher: true, //ture 为老师  false为家长身份
    userInfo: {},
    stuIndex:0,
    physicalReportStu:{},
    headImg:'',
    cycleId:'',
    middleShow:false,
    studentId:'',
    stuName:'',
    iBmStuScore:{
      // level:'',
      // assessmentType: 0,
      // itemId:'',
      // itemName:'BMI',
      // mark:'',
      // score:'',
      // addScore:'',
      // itemUnit:'',
      // weight:'',
      // itemPassRule:'',
      // sg:'',
      // tz:''
    },
    qTStuScore:[],
  },

  getScoreVal(physicalReportStu){
    //console.log('aaaaa='+JSON.stringify(physicalReportStu))
    if(physicalReportStu!=null && physicalReportStu.length>0){
      var list =[]
      for(var i=0;i<physicalReportStu.length;i++){
       
          if(physicalReportStu[i].itemName=='BMI'){ 
           // console.log("aaaaaaaaaaaaaaa="+physicalReportStu[i].itemName);
            var level = 'iBmStuScore.level'
            var assessmentType = 'iBmStuScore.assessmentType'
            var itemId = 'iBmStuScore.itemId'
            var itemName = 'iBmStuScore.itemName'
            var mark = 'iBmStuScore.mark'
            var score = 'iBmStuScore.score'
            var addScore = 'iBmStuScore.addScore'
            var itemUnit = 'iBmStuScore.itemUnit'
            var weight = 'iBmStuScore.weight'
            var itemPassRule = 'iBmStuScore.itemPassRule'
            this.setData({
              [level]:physicalReportStu[i].level,
              [assessmentType]:physicalReportStu[i].assessmentType,
              [itemId]:physicalReportStu[i].itemId,
              [itemName]:physicalReportStu[i].itemName,
              [mark]:physicalReportStu[i].mark,
              [score]:physicalReportStu[i].score,
              [addScore]:physicalReportStu[i].addScore,
              [itemUnit]: physicalReportStu[i].itemUnit,
              [weight]:physicalReportStu[i].weight,
              [itemPassRule]:physicalReportStu[i].itemPassRule,
            })
          }else if(physicalReportStu[i].itemName=='身高'){
            var sg = 'iBmStuScore.sg'
            this.setData({
              [sg]:physicalReportStu[i].mark
            })
          }else if(physicalReportStu[i].itemName=='体重'){
            var tz = 'iBmStuScore.tz'
            this.setData({
              [tz]:physicalReportStu[i].mark
            })
          }else{
            list.push(physicalReportStu[i])
          }
          this.setData({
            qTStuScore:list
          })
      }
      //console.log('aaaaabbb='+JSON.stringify(this.data.qTStuScore))
    }

  },
  //周期切换
  showCycleAlert(){
    this.setData({
      middleShow:true
    })
},

 //头像加载失败
 imgerr(e){
  var ifTeacher = this.data.ifTeacher
  var tHeadImge
  if(ifTeacher){
    tHeadImge = "/images/tx.png"
  }else{
    if(this.data.userInfo.list!=null && this.data.userInfo.list.length>this.data.stuIndex ){       
         if(this.data.userInfo.list[this.data.stuIndex].sex == 1){
           tHeadImge = "/images/tx.png"
         }else{
           tHeadImge = "/images/txn.png"
         }    
    }else{    
      tHeadImge = "/images/tx.png"
    }
  }
  this.setData({  
    headImg:tHeadImge
  })
},


  //弹框选择体测回调方法
  getCycleId(ret){
    console.log("获取数据==="+ret.detail.name);
    console.log("获取数据==="+ret.detail.val);
    this.setData({
      cycleId:ret.detail.val
    })
    if(this.data.ifTeacher){
      this.getPhysicalReportTheacher()
     }else{
      this.getPhysicalReportStu()
     }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ifTeacher:wx.getStorageSync('ifTeacher'), 
      userInfo:wx.getStorageSync('userData'),
      stuIndex:wx.getStorageSync('stuIndex')
    })
    console.log(this.data.ifTeacher+'=用户信息='+JSON.stringify(this.data.userInfo))
    var ifTeacher = this.data.ifTeacher
    if(ifTeacher){
      var headUrl = "/images/tx.png"
      if(this.data.userInfo.headUrl!=null){
        headUrl = this.data.userInfo.headUrl
      }
      this.setData({
        headImg:headUrl
      })
    }else{
      var vname = ""
      var tHeadImge = "/images/tx.png"
      if(this.data.userInfo.list!=null && this.data.userInfo.list.length>0 ){  
        vname = this.data.userInfo.list[this.data.stuIndex].name  
        if(this.data.userInfo.list[this.data.stuIndex].img !=null){
           tHeadImge = this.data.userInfo.list[this.data.stuIndex].img 
        }else{
           if(this.data.userInfo.list[this.data.stuIndex].sex == 1){
             tHeadImge = "/images/tx.png"
           }else{
             tHeadImge = "/images/txn.png"
           }
        }
      }
      console.log(tHeadImge+"===用户信息=="+vname);
      this.setData({
        headImg:tHeadImge,
        stuName:vname
      })
    }



    if(options!=null && options.studentId!=null){
      this.setData({
        studentId:options.studentId
      })  
    }else{
      this.setData({
        studentId:this.data.userInfo.list[this.data.stuIndex].studentId
      })
    }
   this.getTeachCycle()
  },


  //获取教学周期
  getTeachCycle(){
    var that = this
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
  
    app.api.getTeachCycle().then(rest => {
      wx.hideLoading()
      if(rest&&rest.success){   
        console.log('接口数据获取教学周期b='+JSON.stringify(rest.data))

        if(rest.data.list!=null && rest.data.list.length>0){
           that.getTestCycle(rest.data.list[0].key)
        }
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      wx.hideLoading()
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },


  //获取体测周期
  getTestCycle(iTeachCycleId){
    var that = this
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
    var datas = {
      teachCycleId:iTeachCycleId,
      type:"2"
    }
    console.log('接口获取体测周期参数='+JSON.stringify(datas))
    app.api.getTestCycle(datas).then(rest => {
      wx.hideLoading()
      if(rest&&rest.success){   
        console.log('接口数据获取体测周期c='+JSON.stringify(rest.data))

        if(rest.data.list!=null && rest.data.list.length>0){
          that.setData({
             testCycleData:rest.data.list,
             cycleId:rest.data.list[0].key
           })

          //  if(that.data.ifTeacher){
          //   that.getTeacherPrescription()
          //  }else{
          //   that.getPrescription()
          //  }
          if(that.data.ifTeacher){
            that.getPhysicalReportTheacher()
           }else{
            that.getPhysicalReportStu()
           }
        }
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      wx.hideLoading()
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },


getPhysicalReportStu(){
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
  
    var data = {
      sportCycleId:this.data.cycleId,
      studentId:this.data.studentId,
    }
    console.log('家长体质健康报告接口参数='+JSON.stringify(data))
    app.api.getPhysicalReportStu(data).then(rest => {
      wx.hideLoading()
      if(rest&&rest.success){   
        console.log('接口数据家长体质健康报告a='+JSON.stringify(rest))
        this.setData({
          physicalReportStu:rest.data,
        })
        this.getScoreVal(rest.data.studentScore)
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: '网络异常，请稍后重试！！',
          icon: 'none',
          duration: 800,
          mask:true,
          complete: function (res) {
            setTimeout(function(){
              wx.navigateBack({
                delta: 1,  // 返回上一级页面。
                success: function() {           
                }
              })
            },800)
          }
        })
      }
    })
    .catch(r => {
      wx.hideLoading()
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },



  
  getPhysicalReportTheacher(){
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
  
    var data = {
      sportCycleId:this.data.cycleId,
      studentId:this.data.studentId,
    }
    console.log('老师体质健康报告接口参数='+JSON.stringify(data))
    app.api.getPhysicalReportTheacher(data).then(rest => {
      wx.hideLoading()
      if(rest&&rest.success){   
        console.log('接口数据老师体质健康报告a='+JSON.stringify(rest))
        this.setData({
          physicalReportStu:rest.data,
        })
        this.getScoreVal(rest.data.studentScore)
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: '网络异常，请稍后重试！！',
          icon: 'none',
          duration: 800,
          mask:true,
          complete: function (res) {
            setTimeout(function(){
              wx.navigateBack({
                delta: 1,  // 返回上一级页面。
                success: function() {           
                }
              })
            },800)
          }
        })

      }
    })
    .catch(r => {
      wx.hideLoading()
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})